package com.xzy.service;

import com.xzy.common.util.AesUtil;
import com.xzy.common.util.StringUtil;
import com.xzy.entity.ReceiverEntity;
import com.xzy.repository.ReceiverMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 收货人管理
 *
 * @author xzy.xiao
 * @date 2022/8/18  10:28
 */
@Service
@Slf4j
public class ReceiverServiceImpl implements ReceiverService {

    /**
     * 分词的粒度决定了模糊查询的精度，以及附加存储开销
     */
    private final int PHONE_SUB_LENGTH = 4;
    private final int RECEIVER_NAME_SUB_LENGTH = 1;


    private final ReceiverMapper receiverMapper;

    @Autowired
    public ReceiverServiceImpl(ReceiverMapper receiverMapper) {
        this.receiverMapper = receiverMapper;
    }

    /**
     * 保存收货人信息
     *
     * @param entity 收货人信息
     * @return -
     */
    @Override
    public int save(ReceiverEntity entity) {
        // 字段加密
        String encryptedPhone = AesUtil.encrypt(entity.getPhone());
        String encryptedReceiverName = AesUtil.encrypt(entity.getReceiverName());

        // 字段分词 + 加密
        StringBuilder encryptedPhoneFuzzyBuilder = new StringBuilder();
        List<String> phoneSubList = StringUtil.moveBackAndSub(entity.getPhone(), PHONE_SUB_LENGTH);
        for (String phoneSub : phoneSubList) {
            encryptedPhoneFuzzyBuilder.append(AesUtil.encrypt(phoneSub));
        }

        StringBuilder encryptedReceiverNameFuzzyBuilder = new StringBuilder();
        List<String> receiverNameSubList = StringUtil.moveBackAndSub(entity.getReceiverName(), RECEIVER_NAME_SUB_LENGTH);
        for (String receiverNameSub : receiverNameSubList) {
            encryptedReceiverNameFuzzyBuilder.append(AesUtil.encrypt(receiverNameSub));
        }

        entity.setPhone(encryptedPhone);
        entity.setReceiverName(encryptedReceiverName);
        entity.setPhoneFuzzy(encryptedPhoneFuzzyBuilder.toString());
        entity.setReceiverNameFuzzy(encryptedReceiverNameFuzzyBuilder.toString());

        return receiverMapper.save(entity);
    }

    /**
     * 获取所有收货人信息
     *
     * @return -
     */
    @Override
    public List<ReceiverEntity> findAll() {
        return receiverMapper.findAll();
    }

    /**
     * 根据电话号码模糊查询收货人
     *
     * @param phone -
     * @return -
     */
    @Override
    public List<ReceiverEntity> findAllByPhoneLike(String phone) {
        if (phone.length() < PHONE_SUB_LENGTH) {
            throw new IllegalArgumentException();
        }

        String encryptPhone = AesUtil.encrypt(phone);
        log.info("根据号码模糊查询    原号码：{}    加密后的号码：{}", phone, encryptPhone);
        return receiverMapper.findAllByPhoneLike(encryptPhone);
    }

    /**
     * 根据收货人名称模糊查询收货人
     *
     * @param receiverName -
     * @return -
     */
    @Override
    public List<ReceiverEntity> findAllByReceiverNameLike(String receiverName) {
        if (receiverName.length() < RECEIVER_NAME_SUB_LENGTH) {
            throw new IllegalArgumentException();
        }

        String encryptReceiverName = AesUtil.encrypt(receiverName);
        log.info("根据姓名模糊查询    原姓名：{}    加密后的姓名：{}", receiverName, encryptReceiverName);
        return receiverMapper.findAllByReceiverNameLike(encryptReceiverName);
    }

    /**
     * 清空数据库
     *
     * @return -
     */
    @Override
    public int deleteAll() {
        return receiverMapper.deleteAll();
    }
}
